//
//  XMBandbankCardView.m
//  XMJJ_zml
//
//  Created by apple on 2025/5/19.
//

#import "XMBandbankCardView.h"

@interface XMBandbankCardView ()

//@property (nonatomic, strong) UITextField *cardNumTextField;
//@property (nonatomic, strong) UITextField *mobileTextField;
//@property (nonatomic, strong) UITextField *codeTextField;
@property (nonatomic, strong) UIScrollView *bodyScrollView;
@property (nonatomic, strong) UIButton *bankSelectBtn;
@property (nonatomic, strong) UIButton *codeBtn;
@property (nonatomic, strong) UIButton *selectBtn;

@end

@implementation XMBandbankCardView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self creatUI];
    }
    return self;
}

- (void)creatUI {
    [self addSubview:self.navView];
    self.navTitleLabel.text = @"绑定银行卡";
    self.backgroundColor = kColorHEX(0xF7F7F7);
    [self addSubview:self.bodyScrollView];
    
    //基本信息
    NSArray *titleArr = @[@"真实姓名", @"身份证号"];
    NSArray *placeHolderArr = @[@"请填写收款人姓名", @"请填写收款人身份证号"];
    UIView *userInfoBgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, KScaleWidth(179.0))];
    userInfoBgView.backgroundColor = kColorHEX(0xffffff);
    [_bodyScrollView addSubview:userInfoBgView];
    
    for (int i = 0; i < titleArr.count; i ++) {
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(15.0), KScaleWidth(12.0) + (KScaleWidth(32.0) + KScaleWidth(6.0) + KScaleWidth(38.0) + KScaleWidth(5.0))*i, self.width - KScaleWidth(15.0), KScaleWidth(32.0)) font:KFontSize(16.0) textColor:kColorHEX(0x333333)];
        titleLabel.text = titleArr[i];
        [userInfoBgView addSubview:titleLabel];
        
        UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(KScaleWidth(15.0), titleLabel.maxY + KScaleWidth(6.0), self.width - KScaleWidth(15.0)*2, KScaleWidth(38.0))];
        textField.backgroundColor = kColorHEX(0xF4F4F4);
        textField.font = KFontSize(15.0);
        textField.placeholder = placeHolderArr[i];
        [textField setCornerRadius:textField.height/2];
        textField.leftViewMode = UITextFieldViewModeAlways;
        
        UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScaleWidth(15.0), textField.height)];
        textField.leftView = leftView;
        [userInfoBgView addSubview:textField];
    }
    
    UIView *cardInfoTitleView = [[UIView alloc] initWithFrame:CGRectMake(0, userInfoBgView.maxY + KScaleWidth(10.0), self.width, KScaleWidth(100.0))];
    [cardInfoTitleView setGradientLayerWithLocations:@[@(0), @(1.0f)] colors:@[(__bridge id)kColorHEX(0xFFD800).CGColor, (__bridge id)kColorHEX(0xFFF1A2).CGColor] startPoint:CGPointMake(0.47, 0) endPoint:CGPointMake(0.47, 0.5)];
    [_bodyScrollView addSubview:cardInfoTitleView];
    {
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(15.0), KScaleWidth(10.0), cardInfoTitleView.width/2 - KScaleWidth(15.0), KScaleWidth(30.0)) font:KBoldFontSize(16.0) textColor:kColorHEX(0x333333)];
        titleLabel.text = @"银行卡信息";
        [cardInfoTitleView addSubview:titleLabel];
        
        UIButton *photoBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KFontSize(14.0) frame:CGRectMake(cardInfoTitleView.width - KScaleWidth(110.0), KScaleWidth(10.0), KScaleWidth(110.0), titleLabel.height) title:@"拍照识别" titleColor:kColorHEX(0x333333) image:@"mine_bandBank_photo_icon"];
        [photoBtn layoutButtonWithEdgeInsetsStyle:CSButtonEdgeInsetsStyleLeft ImageTitleSpace:KScaleWidth(5.0)];
        [photoBtn addTarget:self action:@selector(photoAction:) forControlEvents:UIControlEventTouchUpInside];
        [cardInfoTitleView addSubview:photoBtn];
    }
    
    //卡信息
    UIView *cardBgView = [[UIView alloc] initWithFrame:CGRectMake(0, userInfoBgView.maxY + KScaleWidth(10.0) + KScaleWidth(48.0), self.width, KScaleWidth(341.0))];
    cardBgView.backgroundColor = kColorHEX(0xffffff);
    [_bodyScrollView addSubview:cardBgView];
    {
        NSArray *titleArr = @[@"银行卡号", @"开户行", @"预留手机号", @"验证码"];
        NSArray *placeHolderArr = @[@"请输入银行卡号", @"请选择银行", @"请输入预留手机号", @"请输入验证码"];
        for (int i = 0; i < titleArr.count; i ++) {
            UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(14.0), KScaleWidth(10.0) + (KScaleWidth(32.0) + KScaleWidth(6.0) + KScaleWidth(38.0) + KScaleWidth(5.0))*i, self.width - KScaleWidth(14.0), KScaleWidth(32.0)) font:KFontSize(16.0) textColor:kColorHEX(0x333333)];
            titleLabel.text = titleArr[i];
            [cardBgView addSubview:titleLabel];
            
            if (i == 1) {
                [cardBgView addSubview:self.bankSelectBtn];
            }
            else {
                UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(KScaleWidth(14.0), titleLabel.maxY + KScaleWidth(6.0), self.width - KScaleWidth(14.0)*2, KScaleWidth(38.0))];
                textField.font = KFontSize(15.0);
                textField.placeholder = placeHolderArr[i];
                textField.backgroundColor = kColorHEX(0xF4F4F4);
                [textField setCornerRadius:textField.height/2];
                textField.placeholder = placeHolderArr[i];
                [cardBgView addSubview:textField];
                
                UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScaleWidth(15.0), textField.height)];
                textField.leftView = leftView;
                textField.leftViewMode = UITextFieldViewModeAlways;
                if (i == 3) {
                    textField.rightViewMode = UITextFieldViewModeAlways;
                    UIView *rightView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScaleWidth(105.0), textField.height)];
                    [rightView addSubview:self.codeBtn];
                    textField.rightView = rightView;
                }
            }
        }
    }
    
    UIButton *confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KBoldFontSize(17.0) frame:CGRectMake((self.width - KScaleWidth(320.0))/2, cardBgView.maxY + KScaleWidth(26.0), KScaleWidth(320.0), KScaleWidth(44.0)) title:@"确认绑定" titleColor:kColorHEX(0x333333)];
    [confirmBtn setGradientLayerWithLocations:@[@(0), @(1.0)] colors:@[(__bridge id)kColorHEX(0xFFD800).CGColor, (__bridge id)kColorHEX(0xFFE44E).CGColor] startPoint:CGPointMake(0.5, 0.37) endPoint:CGPointMake(0.03, 0.37)];
    [confirmBtn setCornerRadius:confirmBtn.height/2];
    [confirmBtn addTarget:self action:@selector(confirmAction:) forControlEvents:UIControlEventTouchUpInside];
    [_bodyScrollView addSubview:confirmBtn];
    
    [_bodyScrollView addSubview:self.selectBtn];
    _selectBtn.y = confirmBtn.maxY + KScaleWidth(14.0);
    
    //服务条款
    UILabel *serviceLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(50.0), _selectBtn.y, self.width - KScaleWidth(50.0), _selectBtn.height)];
    NSMutableAttributedString *serviceAttr = [[NSMutableAttributedString alloc] initWithString:@"登录表示您已同意本平台《服务条款》和《隐私政策》" attributes:@{NSFontAttributeName : KFontSize(13.0)}];
    [serviceAttr setAttributes:@{NSForegroundColorAttributeName : kColorHEX(0x3B5998), NSFontAttributeName : KFontSize(13.0)} range:NSMakeRange(11, 5)];
    [serviceAttr setAttributes:@{NSForegroundColorAttributeName : kColorHEX(0x3B5998), NSFontAttributeName : KFontSize(13.0)} range:NSMakeRange(18, 5)];
    [serviceLabel setAttributedText:serviceAttr];
    [_bodyScrollView addSubview:serviceLabel];
    
    CGFloat width = [@"登录表示您已同意本平台" getWidthWithFontSize:KFontSize(13.0)];
    
    UIButton *serviceBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    serviceBtn.frame = CGRectMake(serviceLabel.x + width, serviceLabel.y, KScaleWidth(90.0), serviceLabel.height);
    [serviceBtn addTarget:self action:@selector(serviceCheckAction:) forControlEvents:UIControlEventTouchUpInside];
//    serviceBtn.backgroundColor = kColorHEX(0xff00ff);
    [_bodyScrollView addSubview:serviceBtn];
    UIButton *privacyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    privacyBtn.frame = CGRectMake(serviceBtn.maxX + KScaleWidth(15.0), serviceBtn.y, serviceBtn.width, serviceBtn.height);
//    privacyBtn.backgroundColor = kColorHEX(0xff00ff);
    [privacyBtn addTarget:self action:@selector(privacyCheckAction:) forControlEvents:UIControlEventTouchUpInside];
    [_bodyScrollView addSubview:privacyBtn];
}

//拍照识别
- (void)photoAction:(UIButton *)button {
    
}

//验证码
- (void)getCodeAction:(UIButton *)button {
    NSLog(@"获取验证码");
}

//确认绑定
- (void)confirmAction:(UIButton *)button {
    
}

- (void)serviceCheckAction:(UIButton *)button {
    
}

- (void)privacyCheckAction:(UIButton *)button {
    
}

- (void)selectAction:(UIButton *)button {
    button.selected = !button.selected;
}

- (void)registerFirst:(UITapGestureRecognizer *)tap {
    [self endEditing:YES];
}

#pragma mark - Init
- (UIScrollView *)bodyScrollView {
    if (!_bodyScrollView) {
        _bodyScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, kNavagationBarH, self.width, self.height - kNavagationBarH)];
        _bodyScrollView.backgroundColor = kColorHEX(0xF7F7F7);
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(registerFirst:)];
        [_bodyScrollView addGestureRecognizer:tap];
    }
    return _bodyScrollView;
}

- (UIButton *)bankSelectBtn {
    if (!_bankSelectBtn) {
        _bankSelectBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KFontSize(15.0) frame:CGRectMake(KScaleWidth(14.0), KScaleWidth(10.0) + KScaleWidth(32.0) + KScaleWidth(6.0) + KScaleWidth(38.0) + KScaleWidth(5.0) + KScaleWidth(6.0) + KScaleWidth(32.0), self.width - KScaleWidth(14.0)*2, KScaleWidth(32.0)) title:@"请选择银行" titleColor:kColorHEX(0x808080)];
        _bankSelectBtn.backgroundColor = kColorHEX(0xF4F4F4);
        [_bankSelectBtn setTitleColor:kColorHEX(0x808080) forState:UIControlStateNormal];
        [_bankSelectBtn setTitleColor:kColorHEX(0x333333) forState:UIControlStateSelected];
        _bankSelectBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        [_bankSelectBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, KScaleWidth(15.0), 0, 0)];
        [_bankSelectBtn setCornerRadius:_bankSelectBtn.height/2];
        
        UIImageView *nextIcon = [[UIImageView alloc] initWithFrame:CGRectMake(_bankSelectBtn.width - KScaleWidth(14.0) - KScaleWidth(10.0), 0, KScaleWidth(10.0), _bankSelectBtn.height)];
        nextIcon.contentMode = UIViewContentModeScaleAspectFit;
        nextIcon.image = ImageNamed(@"mine_detail_icon");
        [_bankSelectBtn addSubview:nextIcon];
    }
    return _bankSelectBtn;
}

- (UIButton *)codeBtn {
    if (!_codeBtn) {
        _codeBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KFontSize(15.0) frame:CGRectMake(0, 0, KScaleWidth(105.0), KScaleWidth(38.0)) title:@"获取验证码" titleColor:kColorHEX(0x3B5998)];
        [_codeBtn addTarget:self action:@selector(getCodeAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _codeBtn;
}

- (UIButton *)selectBtn {
    if (!_selectBtn) {
        _selectBtn = [UIButton buttonWithType:UIButtonTypeCustom frame:CGRectMake(KScaleWidth(23.0), 0, KScaleWidth(26.0), KScaleWidth(26.0)) image:@"login_service_select_icon" selectImg:@"login_service_select_icon_selected"];
        [_selectBtn addTarget:self action:@selector(selectAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _selectBtn;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
